package com.wingsfrog.travel.service.impl;

import com.wingsfrog.travel.modules.dao.TeamDao;
import com.wingsfrog.travel.modules.dao.UserTeamRelDao;
import com.wingsfrog.travel.modules.entity.MainTask;
import com.wingsfrog.travel.modules.entity.TeamInfo;
import com.wingsfrog.travel.modules.entity.UserTeamRel;
import com.wingsfrog.travel.service.MainTaskService;
import com.wingsfrog.travel.service.TaskService;
import com.wingsfrog.travel.service.TeamService;
import com.wingsfrog.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by rankin on 16/3/31.
 */
@Service
public class TeamServiceImpl implements TeamService {
    @Autowired
    private MainTaskService mainTaskService;
    @Autowired
    TeamDao teamDao;
    @Autowired
    UserTeamRelDao userTeamRelDao;
    @Autowired
    TaskService taskService;

    @Override
    public void joinOrCreateTeamByTaskId(Integer userId, Integer mainTaskId) throws Exception {
        //确认是否可以参加
        UserTeamRel rel = userTeamRelDao.findByUserIdAndIsEnable(userId, true);
        if (rel != null) throw new Exception("不能同时参加多个任务");

        MainTask mainTask = mainTaskService.findOneOrFail(mainTaskId);

        //判断是否有队伍
        TeamInfo t = teamDao.findFulllessTeam(mainTaskId);
        if (t != null) {
            //有队伍加入队伍
            t.setExistMemberNum(t.getExistMemberNum() + 1);
            teamDao.save(t);//队伍人数+1
            if (t.getExistMemberNum() > t.getMaxMember()) throw new Exception("越界");
            if (t.getExistMemberNum() == t.getMaxMember()) taskService.accpetTask();

        } else {
            //没队伍创建
            t = new TeamInfo();
            t.setExistMemberNum(1);
            t.setMainTaskId(mainTaskId);
            t.setTeamName("无名小队" + RandomUtil.getRandomString(8));
            t.setMaxMember(mainTask.getMinCount());
            teamDao.save(t);
        }
        UserTeamRel userTeamRel = new UserTeamRel();
        userTeamRel.setTeamId(t.getId());
        userTeamRel.setMainTaskId(mainTaskId);
        userTeamRel.setUserId(userId);
        userTeamRel.setIsEnable(true);
        userTeamRelDao.save(userTeamRel);
    }

    @Override
    public TeamInfo findTeamById(Integer id) throws Exception {
        TeamInfo team = teamDao.findOne(id);
        if (team == null) throw new Exception("team is not exits");
        if (team.getExistMemberNum() < team.getMaxMember()) throw new Exception("没有满人");
        return team;
    }

    @Override
    public TeamInfo findTeamByMainTaskAndUser(Integer mainTaskId, Integer userId) throws Exception {
        UserTeamRel rel = userTeamRelDao.findByMainTaskIdAndUserId(mainTaskId, userId);
        if (rel == null) throw new Exception("team rel is not exits");
        return findTeamById(rel.getTeamId());
    }

    public Long getCountByMainTaskId(Integer mainTaskId) {
        return 3l;
    }

    public List<TeamInfo> getTeamListByMainTaskId(Integer mainTaskId) {
        return new ArrayList<>();
    }
}
